package com.zvfe.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zvfe.common.R;
import com.zvfe.pojo.Menu;
import com.zvfe.pojo.User;
import com.zvfe.service.MenuService;
import com.zvfe.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    //用户登录
    @PostMapping("/login")
    public R login(@RequestBody User user){
        List list = userService.lambdaQuery()
                .eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword()).list();
        if (list.size()>0){
            User user1 =(User) list.get(0);
            List menuList=menuService.lambdaQuery().like(Menu::getMenuright,user1.getRoleId()).list();
            HashMap res=new HashMap();
            res.put("user",user1);
            res.put("menu",menuList);
            return R.success(res);
        }
        return R.err();
    }
    //用户注册
    @PostMapping("/register")
    public R add(@RequestBody User user) {
        return userService.save(user) ? R.success() : R.err();
    }


}
